<template>
  <el-dialog
    :before-close="cancel"
    :close-on-click-modal="false"
    :visible.sync="insideVisible"
    class="vc-dialog-form"
    title="修改角色"
    width="480px"
  >
    <!-- <supForm :formConfig="rightClickConfig.addChildFormConfig" ref="addChildForm" /> -->
    <el-form :model="editForm" :rules="rules" label-width="100px" ref="editForm" size="small">
      <el-form-item label="上级角色组" prop="rgroupId">
        <el-tree-select
          :data="insideTreeData"
          :defaultExpandedKeys="editForm.rgroupId ? [editForm.rgroupId] : []"
          :props="defaultProps"
          clearable
          v-model="editForm.rgroupId"
        ></el-tree-select>
      </el-form-item>

      <el-form-item label="角色名称" prop="name">
        <el-input clearable maxlength="20" v-model="editForm.name"></el-input>
      </el-form-item>

      <el-form-item label="角色描述" prop="descr" style="text-align:left">
        <el-input clearable maxlength="100" rows="2" type="textarea" v-model="editForm.descr"></el-input>
      </el-form-item>
    </el-form>

    <span class="dialog-footer" slot="footer">
      <el-button @click="cancel" size="small">取消</el-button>
      <el-button :disabled="disabled" @click="save" size="small" type="primary">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import supForm from '_c/form'
export default {
  name: 'editRoleGroup',
  components: { supForm },
  props: {
    editRoleDialogVisible: {
      type: Boolean,
      default () {
        return false
      }
    },
    rgTreeDataHasTop: {
      type: Array,
      default () {
        return []
      }
    },
    id: {
      type: String,
      default () {
        return ''
      }
    },
    value: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      insideTreeData: [],
      insideVisible: false,
      editForm: {
        id: '',
        name: '',
        pid: ''
      },
      rules: {
        rgroupId: [
          {
            required: true,
            message: '请选择上级角色组',
            trigger: ['blur', 'change']
          }
        ],
        name: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          },
          {
            pattern: /^\S+(\s+\S+)*$/,
            message: '不能以空格开头或者结尾',
            trigger: 'blur'
          }
        ]
      },
      disabled: false,
      readonly: true,
      defaultProps: {
        id: 'id',
        label: 'text',
        children: 'children',
        isLeaf: 'isLeaf'
      }
    }
  },
  mounted () {
    // this.initTreeData()
  },
  computed: {
    name () {
      return this.$parent.editFormData.name
    },
    rgroupId () {
      return this.$parent.editFormData.pid
    }
  },
  watch: {
    editRoleDialogVisible (value) {
      if (value) {
        this.initVisible()
        this.initTreeData()
      }
      this.initflag(value)
      setTimeout(() => {
        this.$refs.editForm.clearValidate();
      }, 0)
    },
    rgTreeDataHasTop (value) {
      this.initTreeData(value)
    }
  },
  methods: {
    /*
      initflag
    */
    initflag (val) {
      this.insideTreeData = this.rgTreeDataHasTop
      this.editForm = this.value
      this.insideVisible = val
    },
    /*
      initTreeData
    */
    initTreeData (val) {
      this.insideTreeData = []
      this.insideTreeData = this.rgTreeDataHasTop
    },
    /**
     * 保存
     */
    save () {
      if (this.disabled) {
        return
      }
      this.$refs.editForm.validate(valid => {
        if (!valid) {
          return false
        }
        this.disabled = true
        this.$api
          .patchRoleById({
            data: this.editForm
          })
          .then(res => {
            this.disabled = false
            if (res.success) {
              this.$emit('close', res.data || {})
              this.$notifyBox('修改成功', 'success')
            }
          })
          .catch(err => {
            this.disabled = false
          })
      })
    },
    /* 初始化编辑 */
    initVisible () {
      this.editForm = this.value
    },
    /**
     * 取消
     */
    cancel () {
      this.$emit('close', false)
    }
  }
}
</script>
